﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace TextMIS
{
    public partial class frmEditPwd : Form
    {
        public frmEditPwd()
        {
            InitializeComponent();
        }

        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;

        private void btnOk_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from UserInfo where UserName='" + txtUserName.Text.Trim() + "' and UserPassword='" + txtOldPwd.Text.Trim() + "'", conn);
            DataSet ds = new DataSet();

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "UserInfo");
            conn.Close();

            if (ds.Tables["UserInfo"].Rows.Count == 0)
            {
                MessageBox.Show("用户旧密码输入错误，请重新输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserName.Focus();
                return;
            }
            if (txtNewPwd.Text.Trim() != txtReNewPwd.Text.Trim())
            {
                MessageBox.Show("两次新密码输入不一致，请重新输入！");
                txtReNewPwd.Focus();
                return;
            }
            conn.Close();

            com = new SqlCommand("update UserInfo set UserPassword='" + txtNewPwd.Text.Trim() + "' where UserName='" + txtUserName.Text.Trim() + "'", conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("恭喜你！密码修改成功！");

        }

        private void frmEditPwd_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(frmLogin.SQLstr);
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.OK; ;
        }
    }
}
